<template>
  <div class="login_page">
    <div class="logo_box">
      <img class="logo_img" src="@/assets/login/logo.png" alt="">
    </div>
    <div class="login_box">
      <img class="login_img" src="../assets/login/login.png" alt="">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login_form"
      >
        <h3 class="title">产融数字服务管理后台</h3>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
            :clearable="true"
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            :clearable="true"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled">
          <div class="get_code_line">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              class="code_input"
              :clearable="true"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon
                slot="prefix"
                icon-class="validCode"
                class="el-input__icon input-icon"
              />
            </el-input>
            <div class="login_code">
              <img :src="codeUrl" @click="getCode" class="login-code-img" />
            </div>
          </div>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            class="submit_btn"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right" v-if="register">
            <router-link class="link-type" :to="'/register'"
              >立即注册</router-link
            >
          </div>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2024 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import '@/assets/styles/px2rem.scss';
@import '@/assets/styles/variables.scss';

.login_page {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: url("../assets/login/login_bg.png") no-repeat 0 0;
  background-size: 100% 100%;
  .logo_box {
    position: fixed;
    top: px2rem(50);
    left: px2rem(50);
    z-index: 2;
    .logo_img {
      display: block;
      width: px2rem(200);
      height: px2rem(62);
    }
  }
  .login_box {
    display: flex;
    align-items: center;
    .login_img {
      display: block;
      width: px2rem(540);
      height: px2rem(596);
      border-radius: px2rem(20) 0 0 px2rem(20);
    }
    .login_form {
      background: #ffffff;
      width: px2rem(540);
      height: px2rem(596);
      padding: px2rem(90) px2rem(48) px2rem(5);
      box-sizing: border-box;
      border-radius: 0 px2rem(20) px2rem(20) 0;
      .title {
        margin: px2rem(0) auto px2rem(30) auto;
        text-align: center;
        color: $theme;
        font-size: px2rem(36);
        font-weight: 600;
        margin-bottom: px2rem(70);
      }
      ::v-deep .el-form-item {
        &:last-child {
          margin-bottom: 0;
        }
      }
      .input-icon {
        height: px2rem(39);
        width: px2rem(14);
        margin-left: px2rem(2);
      }
      .login-tip {
        font-size: 13px;
        text-align: center;
        color: #bfbfbf;
      }
      .get_code_line {
        display: flex;
        justify-content: space-between;
        .code_input {
          flex: 1 1 auto;
          margin-right: px2rem(20);
        }
        .login_code {
          width: 33%;
          height: 35px;
          display: flex;
          justify-content: flex-end;
          align-items: center;
          .login-code-img {
            display: block;
            height: 35px;
            cursor: pointer;
          }
        }
      }

      .submit_btn {
        width: 100%;
        background: linear-gradient( 90deg, #0058FF 0%, #69ADFF 100%);
        height: px2rem(48);
        border: none;
        font-size: 18px;
        font-weight: 600;
      }
    }
  }
  .el-login-footer {
    height: px2rem(40);
    line-height: px2rem(40);
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: px2rem(1);
  }
}
</style>
